﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.ComponentModel;

namespace FloodFree.Phone.ViewModels
{
    public class SensorViewModel : BaseViewModel
    {

        private ItemViewModel cityViewModel;


        public SensorViewModel()
        {
            this.Items = new ObservableCollection<SensorItemViewModel>();
        }

        public ObservableCollection<SensorItemViewModel> Items { get; private set; }

        public ItemViewModel CityViewModel
        {

            get
            {
                return cityViewModel;
            }
            set
            {
                cityViewModel = value;
                NotifyPropertyChanged("CityViewModel");
                NotifyPropertyChanged("CityName");

            }
        }
        
        public bool IsDataLoaded
        {
            get;
            private set;
        }

        public void LoadData(ItemViewModel cityModel)
        {
            this.CityViewModel = cityModel;
            this.Items.Clear();

            string WS_URL = "http://floodfree.cloudapp.net/api/cities/Details/" + cityModel.Id.ToString();

            Uri uri = new Uri(WS_URL, UriKind.Absolute);
            WebClient client = new WebClient();
            client.DownloadStringCompleted += (source, args) =>
            {
                if (args.Error != null)
                {
                    MessageBox.Show("Erro ao buscar os dados.");
                    return;
                }

                List<SensorItemViewModel> cities = SensorsFromJson(args.Result);
                foreach (SensorItemViewModel item in cities)
                {
                    this.Items.Add(item);
                }
                this.IsDataLoaded = true;

            };
            client.DownloadStringAsync(uri);
        }

        private List<SensorItemViewModel> SensorsFromJson(String json)
        {
            List<SensorItemViewModel> sensors = new List<SensorItemViewModel>();
            JObject o = JObject.Parse(json);
            JArray citiesJson = (JArray)o["Sensors"];
            foreach (JObject sensorJson in citiesJson)
            {
                SensorItemViewModel sensor = new SensorItemViewModel
                {
                    Id = (string)sensorJson["SensorUUID"],
                    Address = (string)sensorJson["Address"],
                    Latitude = (string)sensorJson["Latitude"],
                    Longitude = (string)sensorJson["Longitude"],
                    LastStatus = ((int)sensorJson["LastStatus"]).ToString(),
                    LastStatusDate = (string) sensorJson["LastStatusDate"]
                };
                sensors.Add(sensor);
            }


            return sensors;
        }

    }
}
